﻿using WMLogistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DapperExtensions;
using DapperExtensions.Sql;
using DapperExtensions.Mapper;
using System.Reflection;
using Dapper;
using DapperExtensions;

namespace WMLogistics_Dao
{
    public class ContractCancelDao
    {
        private SqlGeneratorImpl sqlGenerator;
        public ContractCancelDao()
        {
            var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
            sqlGenerator = new SqlGeneratorImpl(config);
            DapperExtensions.DapperExtensions.Configure(config);
        }
        public int Add(wuliu_contract_cancel cancel)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {

                return conn.Insert<wuliu_contract_cancel>(cancel);
            }
        }
        public PageData<wuliu_contract_cancel> GetPageList(IPredicate predicate, IPredicate wlPredicate, IList<ISort> sorts, int pageIndex, int pageSize)
        {

            var pageData = new PageData<wuliu_contract_cancel>();
            var sql = "select * from wuliu_contract_cancel  where wuliu_id in  (select wuliu_id from wuliu_wlcompany_base ";

            DynamicParameters wlParam = new DynamicParameters();
            var wlSql = GetWhereSql(wlPredicate, wlParam);
            if (string.IsNullOrEmpty(wlSql))
                sql += string.Format(sql, "where " + wlSql);
            sql += ")";

            DynamicParameters dynamicParameters = new DynamicParameters();
            var whereSql = GetWhereSql(predicate, dynamicParameters);
            if (string.IsNullOrEmpty(whereSql))
                sql += " and " + whereSql;

            dynamicParameters.AddDynamicParams(wlParam);
            dynamicParameters.Add("pageIndex", pageIndex*pageSize);
            dynamicParameters.Add("pageSize", pageSize);
            using (var conn = ConnectionHelper.OpenConnection())
            {
                var count = conn.Query<wuliu_contract_cancel>(sql, dynamicParameters).Count();
                pageData.Items = conn.Query<wuliu_contract_cancel>(sql, dynamicParameters).ToList(); ;
                pageData.CurrentPage = pageIndex;
                pageData.TotalPageCount = pageData.TotalNum % pageSize == 0 ? pageData.TotalNum / pageSize : pageData.TotalNum / pageSize + 1;
            }
            return pageData;

        }
        private string GetWhereSql(IPredicate predicate, DynamicParameters dynamicParameters)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            var wherSql = predicate.GetSql(sqlGenerator, parameters);


            foreach (var parameter in parameters)
            {
                dynamicParameters.Add(parameter.Key, parameter.Value);
            }
            return wherSql;
        }

        public wuliu_contract_cancel Get(int id)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
               return conn.Get<wuliu_contract_cancel>(id);
            }
        }
    }


}
